﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using DLL.Entities;
using System.Timers;

namespace MIS
{
    public partial class Form1 : Form
    {
        int CmissileNum = 5;
        public Form1()
        {
            InitializeComponent();
            //Reads the config file.
            Missile missile = new Missile();
            XmlSerializer reader = new XmlSerializer(typeof(Missile));
            missile = (Missile)Utility.XMLRead("../../../DLL/XML_FILES/MIS_Config.xml", reader);
            //Set amount of missiles to form on load
            txtMIS_Num.Text = Convert.ToString(missile.missileNum);                     
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            misFirePic.Visible = false;
        }

        private void txtMIS_Num_TextChanged(object sender, EventArgs e)
        {

        }
        #region MissileEvent_Timers
        private void EventReader_Tick(object sender, EventArgs e)
        {
            //Checks every second if COM_Event has changed the number of missiles by firing
            Missile missileNum_Checker = new Missile();
            XmlSerializer reader = new XmlSerializer(typeof(Missile));
            missileNum_Checker = (Missile)Utility.XMLRead("../../../COM/COM_Event/COM_Event.xml", reader);

            if (CmissileNum != Convert.ToInt16(missileNum_Checker.missileNum))
            {
                //converts current missile num to that of the missile num in COM_Event for next update
                CmissileNum = missileNum_Checker.missileNum;
                txtMIS_Num.Text = Convert.ToString(CmissileNum);
                Missile missileEvent = new Missile();

                //writes to MIS_Event that the missile has been fired.
                missileEvent.missileNum = CmissileNum;
                missileEvent.missilefire = "Missile fired!";
                XmlSerializer MISreader = new XmlSerializer(typeof(Missile));
                string fileSave = "../../XML/MIS_Event.xml";
                Utility.WriteXML(missileEvent, fileSave, MISreader);

                //starts timer and make picture visible.
                MissileFiring.Enabled = true;
                misFirePic.Visible = true;
                misFirePic.BackgroundImage = Image.FromFile(@"../../Images/MDPMissileFire.JPG");
                misFirePic.BackgroundImageLayout = ImageLayout.Stretch;
            }
        }

        private void StatusUpdate_Tick(object sender, EventArgs e)
        {
            //Updates MIS_Status with the current time for COM to read.
            Status missileStatus = new Status();
            missileStatus.missileStatus = Convert.ToString(DateTime.Now);
            
            XmlSerializer serializer = new XmlSerializer(typeof(Status));
            string fileSave = "../../XML/MIS_Status.xml";
            Utility.WriteXML(missileStatus, fileSave, serializer); 
        }

        private void MissileFiring_Tick(object sender, EventArgs e)
        {
            misFirePic.Visible = false;
            MissileFiring.Enabled = false;
        }
        #endregion
    }
}
